package org.jsola.hr.web;

import org.jsola.common.ArrayKit;
import org.jsola.common.StrKit;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpTagDTO;
import org.jsola.hr.query.CompanyIdSQuery;
import org.jsola.hr.service.IEmpTagService;
import org.jsola.hr.vo.EmpTagVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author wcl
 */
@RestController("hrEmpTagController")
@RequestMapping(HrConstants.API_PATH + "/v1/empTag")
public class EmpTagController implements IEmpTagControllerAPI {

    @Autowired
    private IEmpTagService empTagService;

    @Override
    @PostMapping
    public Result<EmpTagVO> save(@Valid @RequestBody EmpTagDTO empTagDTO) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                empTagService.save(empTagDTO, tokenUser)
        );
    }

    @Override
    @DeleteMapping
    public Result<Integer> delete(@Valid @RequestBody EmpTagDTO empTagDTO) {
        TokenUser tokenUser = UserContext.getUser();

        int count = empTagService.delete(tokenUser, empTagDTO);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping("/listByCompanyId")
    public Result<List<EmpTagVO>> listByCompanyId(String companyId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                empTagService.listByCompanyId(companyId, tokenUser)
        );
    }

    @Override
    @GetMapping("/listByCompanyIds")
    public Result<List<EmpTagVO>> listByCompanyIds(String companyIds) {
        TokenUser tokenUser = UserContext.getUser();
        List<EmpTagVO> empTagVOList = new ArrayList<>();
        if (StrKit.isEmpty(companyIds)) {
            // 查询全部标签
            empTagVOList = empTagService.listBySiteId(tokenUser);
        } else {
            // 根据公司ids查询标签
            List<String> companyIdList = ArrayKit.split(companyIds, ",");
            for (String companyId : companyIdList) {
                List<EmpTagVO> empTagList =  empTagService.listByCompanyId(companyId, tokenUser);
                if (!CollectionUtils.isEmpty(empTagList)) {
                    empTagVOList.addAll(empTagList);
                }
            }

        }
        return Result.success(
                empTagVOList
        );
    }

    @Override
    @PostMapping("/listByCompanyIds")
    public Result<List<EmpTagVO>> listByCompanyIds(@RequestBody CompanyIdSQuery companyIds) {
        TokenUser tokenUser = UserContext.getUser();
        List<EmpTagVO> empTagVOList = new ArrayList<>();
        if (companyIds == null || StrKit.isEmpty(companyIds.getCompanyIds())) {
            // 查询全部标签
            empTagVOList = empTagService.listBySiteId(tokenUser);
        } else {
            // 根据公司ids查询标签
            List<String> companyIdList = ArrayKit.split(companyIds.getCompanyIds(), ",");
            // 去重
            companyIdList = companyIdList.parallelStream().distinct().collect(Collectors.toList());
            for (String companyId : companyIdList) {
                List<EmpTagVO> empTagList =  empTagService.listByCompanyId(companyId, tokenUser);
                if (!CollectionUtils.isEmpty(empTagList)) {
                    empTagVOList.addAll(empTagList);
                }
            }

        }
        empTagVOList = empTagVOList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(EmpTagVO::getTagName))), ArrayList::new)
        );
        return Result.success(
                empTagVOList
        );
    }


}
